//
//  PersonViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/3.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "PersonViewController.h"

@interface PersonViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (strong, nonatomic) NSArray * sectionAImgArr;
@property (strong, nonatomic) NSArray * sectionATitleArr;
@property (strong, nonatomic) NSArray * sectionBImgArr;
@property (strong, nonatomic) NSArray * sectionBTitleArr;
@property (strong, nonatomic) NSArray * sectionCImgArr;
@property (strong, nonatomic) NSArray * sectionCTitleArr;
@property (nonatomic,copy) NSString * userName;

@end

@implementation PersonViewController

static NSString * cellID = @"cell";
static NSString * PersonHeaderTableViewCellID = @"PersonHeaderTableViewCell";
static NSString * PersonTableViewCellID = @"PersonTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self wr_setNavBarShadowImageHidden:NO];
    [self creatTableview];
    [self registCell];
    [self wr_setStatusBarStyle:UIStatusBarStyleLightContent];
    [self wr_setNavBarBarTintColor:SFMNavBarColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeImg:) name:@"personDidChangeImg" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeName:) name:@"personDidChangeName" object:nil];
}

#pragma mark - 修改图片成功
-(void)didChangeImg:(NSNotification *)notice
{
    
}

#pragma mark - 修改姓名成功
-(void)didChangeName:(NSNotification *)notice
{
    NSDictionary * dic = notice.userInfo;
    NSString * name = dic[@"name"];
    self.userName = name;
    WeakSelf;
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.tableview reloadData];
    });
}

-(void)registCell
{
    [self.tableview registerClass:[PersonHeaderTableViewCell class] forCellReuseIdentifier:PersonHeaderTableViewCellID];
    [self.tableview registerClass:[PersonTableViewCell class] forCellReuseIdentifier:PersonTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-kTabBarH) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        tableview.contentInset = UIEdgeInsetsMake(-kStatusBarH, 0, 0, 0);
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 148;
        }
    }
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 ){
        return self.sectionATitleArr.count + 1;
    }
    else if( section == 1) {
        return 2;
    }else if (section == 2)
    {
        return 4;
    }
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            PersonHeaderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PersonHeaderTableViewCellID];
            NSString * headurl = [[NSUserDefaults standardUserDefaults] objectForKey:@"headimgurl"];
            [cell.img sd_setImageWithURL:[NSURL URLWithString:headurl] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                if (image != nil) {
                    cell.img.image = image;
                }else
                {
                    cell.img.backgroundColor = SFMRandColor;
                }
            }];
            NSString * loginname = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginname"];
            cell.nameLabel.text = self.userName?self.userName:loginname;
            return cell;
        }
    }
    PersonTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PersonTableViewCellID];
    if (indexPath.section == 0) {
        if (indexPath.row != 0) {
            cell.img.image = [self.sectionAImgArr objectAtIndex:indexPath.row -1];
            cell.nameLabel.text = [self.sectionATitleArr objectAtIndex:indexPath.row - 1];
            if (indexPath.row == 1) {
                cell.img.contentMode = UIViewContentModeScaleAspectFill;
                cell.img.clipsToBounds = YES;
            }
        }
    }else if (indexPath.section == 1)
    {
        cell.img.image = [self.sectionBImgArr objectAtIndex:indexPath.row];
        cell.nameLabel.text = [self.sectionBTitleArr objectAtIndex:indexPath.row];
//        if(indexPath.row == 0)
//        {
//            NSString * expiredLincence = @"3张即将过期";
//            CGFloat width = [expiredLincence widthWithFont:font(10) constrainedToHeight:25];
//            cell.detailLabel.frame = CGRectMake(WIDTH - width - 55, 17.5, width + 15, 25);
//            cell.detailLabel.text = expiredLincence;
//        }else if(indexPath.row == 1)
//        {
//            NSString * expiredContract = @"3张即将过期";
//            CGFloat width = [expiredContract widthWithFont:font(10) constrainedToHeight:25];
//            cell.detailLabel.frame = CGRectMake(WIDTH - width - 55, 17.5, width + 15, 25);
//            cell.detailLabel.text = expiredContract;
//        }else
//        {
//            cell.detailLabel.text = @"";
//        }
    }else
    {
        cell.img.image = [self.sectionCImgArr objectAtIndex:indexPath.row];
        cell.nameLabel.text = [self.sectionCTitleArr objectAtIndex:indexPath.row];
        cell.detailLabel.text = @"";
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 0)
    {
        if ([SFMusertype isEqualToString:@"2"]) {
            if(indexPath.row == 1){
                WeakSelf;
                NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
                NSString *path=[docPath stringByAppendingPathComponent:@"person.archiver"];
                NSArray * arr =  [NSKeyedUnarchiver unarchiveObjectWithFile:path];
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"请选择公司名称" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                for (authListModel * model in arr) {
                    UIAlertAction * action = [UIAlertAction actionWithTitle:model.fgsname style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        SecurityGuardRecordViewController * ctr = [[SecurityGuardRecordViewController alloc] init];
                        ctr.companyName = model.fgsname;
                        [weakSelf.navigationController pushViewController:ctr animated:YES];
                    }];
                    [alert addAction: action];
                }
                UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                }];
                [alert addAction: action1];
                [self presentViewController:alert animated:YES completion:nil];
                    
                
            }else if(indexPath.row == 2)
            {
                PayMoneyDetailViewController * ctr = [[PayMoneyDetailViewController alloc] init];
                [self.navigationController pushViewController:ctr animated:YES];
            }else if (indexPath.row == 3) {
                MyPropertyUnitViewController * ctr = [[MyPropertyUnitViewController alloc] init];
                [self.navigationController pushViewController:ctr animated:YES];
            }else
            {
                
            }
        }else
        {
            if(indexPath.row == 1)
            {
                PayMoneyRecodeViewController * ctr = [[PayMoneyRecodeViewController alloc] init];
                [self.navigationController pushViewController:ctr animated:YES];
            }else if (indexPath.row == 2) {
                MyPropertyUnitViewController * ctr = [[MyPropertyUnitViewController alloc] init];
                [self.navigationController pushViewController:ctr animated:YES];
            }else
            {
                
            }
        }
        
    }else if (indexPath.section == 1)
    {
        if (indexPath.row == 0) {
            MyBusineseLicenceViewController * ctr =[[MyBusineseLicenceViewController alloc] init];
            [self.navigationController pushViewController:ctr animated:YES];
        }else if (indexPath.row == 1)
        {
            MyContractViewController * ctr = [[MyContractViewController alloc] init];
            [self.navigationController pushViewController:ctr animated:YES];
        }else
        {
//            MyCertifyViewController * ctr = [[MyCertifyViewController alloc] init];
//            [self.navigationController pushViewController:ctr animated:YES];
            ReallyNameCertifyViewController * ctr = [[ReallyNameCertifyViewController alloc] init];
            [self.navigationController pushViewController:ctr animated:YES];
        }
    }else
    {
        if (indexPath.row == 0) {
            
        }else if (indexPath.row == 1)
        {
            MyComplainViewController * ctr = [[MyComplainViewController alloc] init];
            [self.navigationController pushViewController:ctr animated:YES];
        }else if(indexPath.row == 2)
        {
            MyRepariViewController * ctr = [[MyRepariViewController alloc] init];
            [self.navigationController pushViewController:ctr animated:YES];
        }else
        {
            WeakSelf;
            SetViewController * ctr = [[SetViewController alloc] init];
            ctr.loadOutEventBlock = ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];

                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"gbuserid"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"headimgurl"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loginname"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"usertype"];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Id"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    LoginViewController * load = [[LoginViewController alloc] init];
                    load.DidLoginEventBlock = ^{
                        
                    };
                    [weakSelf presentViewController:load animated:YES completion:nil];
                });
            };
            [self.navigationController pushViewController:ctr animated:YES];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    line.backgroundColor = SFMColor(239, 239, 244);
    return line;
}

-(NSArray *)sectionAImgArr
{
    if (_sectionAImgArr == nil) {
        if ([SFMusertype isEqualToString:@"2"]) {
            _sectionAImgArr = [NSArray arrayWithObjects:[UIImage imageNamed:@"WechatIMG1006"],[UIImage imageNamed:@"pay-list"],[UIImage imageNamed:@"property"], nil];
        }else
        {
            _sectionAImgArr = [NSArray arrayWithObjects:[UIImage imageNamed:@"pay-list"],[UIImage imageNamed:@"property"], nil];
        }
    }
    return _sectionAImgArr;
}

-(NSArray *)sectionATitleArr
{
    if (_sectionATitleArr == nil) {
        if ([SFMusertype isEqualToString:@"2"]) {
            _sectionATitleArr = [NSArray arrayWithObjects:@"巡检记录",@"缴费清单",@"物业单元", nil];
        }else
        {
            _sectionATitleArr = [NSArray arrayWithObjects:@"缴费清单",@"物业单元", nil];
        }
    }
    return _sectionATitleArr;
}

-(NSArray *)sectionBImgArr
{
    if (_sectionBImgArr == nil) {
        _sectionBImgArr = [NSArray arrayWithObjects:[UIImage imageNamed:@"property"],[UIImage imageNamed:@"contract"], nil];//[UIImage imageNamed:@"certification"],
    }
    return _sectionBImgArr;
}

-(NSArray *)sectionBTitleArr
{
    if (_sectionBTitleArr == nil) {
        _sectionBTitleArr = [NSArray arrayWithObjects:@"我的营业执照",@"我的合同", nil];//@"实名认证",
    }
    return _sectionBTitleArr;
}

-(NSArray *)sectionCImgArr
{
    if (_sectionCImgArr == nil) {
        _sectionCImgArr = [NSArray arrayWithObjects:[UIImage imageNamed:@"property"],[UIImage imageNamed:@"complaints"],[UIImage imageNamed:@"record"],[UIImage imageNamed:@"set"], nil];
    }
    return _sectionCImgArr;
}

-(NSArray *)sectionCTitleArr
{
    if (_sectionCTitleArr == nil) {
        _sectionCTitleArr = [NSArray arrayWithObjects:@"问卷调查",@"我投诉的",@"我的报修",@"设置", nil];
    }
    return _sectionCTitleArr;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y>=0) {
        [self wr_setStatusBarStyle:UIStatusBarStyleLightContent];
    }else
    {
        [self wr_setStatusBarStyle:UIStatusBarStyleDefault];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"personDidChangeImg" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"personDidChangeName" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
